export function jsonToTableOptions (widgetList, config) {
  let tableList = []
  widgetList.forEach((item) => {
    if (item.type === "slot") return
    if (item.type === "grid") {
      item.cols.forEach((col) => {
        tableList = [
          ...tableList,
          ...jsonToTableOptions(col.widgetList, config),
        ]
      })
    } else {
      const optionItems = item.options.optionItems || []
      tableList.push({
        label: item.options.label,
        prop: item.options.name,
        sort: config.sort.findIndex((i) => i === item.options.name),
        sortable: config.sortable.includes(item.options.name),
        type: item.type || item.options.type,
        optionItems: optionItems.map(ele => ({
          value: ele.value,
          text: ele.label
        }))
      })
    }
  })
  return tableList.sort((a, b) => {
    return a.sort - b.sort
  })
}
export const useFormJson = (
  formJson,
  config = {
    sort: [],
    sortable: [],
  }
) => {
  const ops = jsonToTableOptions(formJson.widgetList, config)
  return ops
}
